$(document).ready(function(){
       
        $("#seeIndex").on('click',function(){
            seeIndex()
        })
        
        seeIndex()
        function seeIndex(){
            $.ajax({
                url:'/admin/indexUserSum',
                type:'get',
                success:function(res){
                    console.log(res.userNum)
                //     var html='';
                //     html+=`
                //         <p>${res.userNum}</p>
                //    `
                //     $('.userSum').html(html)

                    $.ajax({
                        url:'/admin/indexarticleSum',
                        type:'get',
                        success:function(res1){
                            console.log(res1.articleNum)
                            var html='';
                            html+=`
                            <div class="Admin-index">
                                <h3>已注册用户的总数量</h3>
                                <div class="userSum">${res.userNum}</div>
                                <h3>发表的文章总数</h3>
                                <div class="articleSum">${res1.articleNum}</div>
                            </div>
                        `
                            $('.mainMiddle').html(html)
                        }
                    })
                }
            })     
            
            
        }
           
        //删除用户函数
        function deleUser(id,tar){
            $.ajax({
                url:'/admin/deleU',
                type:'post',
               data:{
                id:id,
               },
                success:function(res){
                    console.log(res)
                   $(tar).parent().parent().remove()
                },
                error:function(res){
                    console.log(res)
                }

            })
        }
        
              
            //查询用户
        $('#seachUser').on('click',function(){
            console.log(111)
            $.ajax({
                url:'/admin/searchUser',
                type:'get',
                success:function(res){
                   // console.log(res)
                    var html='';
                    html+=`
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有用户</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i> 用户ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 用户名</th>
                                    <th><i class="fa fa-bookmark">注册时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
            `
            $('.mainMiddle').html(html)
            var html1=''
            for(var i=0;i<res.seachUser.length;i++){
                html1+=`
                <tr class="${res.seachUser[i]._id}">
                <td><a href="javascript:;">${res.seachUser[i]._id}</a></td>
                <td class="hidden-phone">${res.seachUser[i].username}</td>
                <td>${res.seachUser[i].regDate}</td>
                <td><span class="label label-info label-mini">Due</span></td>
                <td>
                    <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                    <button class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></button>
                    <button class="btn btn-danger btn-xs deleU"><i class="fa fa-trash-o "></i></button>
                </td>
            </tr>
                `
            }
                $('.mainMiddle .content-panel tbody').html(html1)
                //查询成功后删除用户
                $('.deleU').on('click',function(evt){
                    var id=$(evt.target).parent().parent().attr('class')
                    deleUser(id,evt.target)
                    
                    
                })


                },
                error:function(res){
                    console.log(res)
                }
                
            })
        })
        //查询文章
        $('#searchArcticle').on('click',function(){
            $.ajax({
                url:'/admin/searchAtic',
                type:'get',
                success:function(res){
                   // console.log('1qqq')
                    console.log(res)
                    var html='';
                    html+=`
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有文章</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i>文章ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 文章标题</th>
                                    <th><i class="fa fa-bookmark">文章发布时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
            `
            $('.mainMiddle').html(html)
            var html1=''
            for(var i=0;i<res.Allarcticle.length;i++){
                html1+=`
                <tr class="${res.Allarcticle[i]._id}">
                <td><a href="javascript:;">${res.Allarcticle[i]._id}</a></td>
                <td class="hidden-phone">${res.Allarcticle[i].arctitle}</td>
                <td>${res.Allarcticle[i].pubtime}</td>
                <td><span class="label label-info label-mini">Due</span></td>
                <td>
                    <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                    <button class="btn btn-primary btn-xs changeA"><i class="fa fa-pencil"></i></button>
                    <button class="btn btn-danger btn-xs deleA"><i class="fa fa-trash-o "></i></button>
                </td>
            </tr>
                `
            }
                $('.mainMiddle .content-panel tbody').html(html1)
                //查询文章成功后删除文章
                $('.deleA').on('click',function(evt){
                    var id=$(evt.target).parent().parent().attr('class')
                    
                    $.ajax({
                        url:'/admin/deleArticle',
                        type:'post',
                       data:{
                        id:id,
                       },
                        success:function(res){
                            console.log(res)
                           $(evt.target).parent().parent().remove()
                        },
                        error:function(res){
                            console.log(res)
                        }

                    })
                })//删除文章结束
                //修改文章
                $('.changeA').on('click',function(evt){
                    
                    var id=$(evt.target).parent().parent().attr('class')
                    $('.mengban').css({
                        "display":"block"
                    })
                    $('.del').on('click',function(){
                        $('.mengban').css({
                            'display':'none'
                        })
                    })
                    //查询特定的文章
                    console.log(id)
                    $.ajax({
                        url:'/admin/readArc',
                        type:'post',
                        data:{
                            id:id,
                        },
                        success:function(res){
                            console.log(res)
                            console.log(res.data[0].arctitle,res.data[0].content)
                            $('.uptitle').val(res.data[0].arctitle)
                             ue.setContent(res.data[0].content);
                             
                             //查询文章之后修改文章
                             $('.upBtn').on('click',function(){
                                var arctitle=$('.uptitle').val()
                                var content=ue.getContent()
                                console.log(content,arctitle)
                                $.ajax({
                                    url:'/admin/uploadArc',
                                    type:'post',
                                    data:{
                                        id:id,
                                       arctitle:arctitle,
                                       content:content
                                    },
                                    success:function(res){
                                       console.log(res)
                                    }
                                })
                                $('.mengban').css({
                                    'display':'none'
                                })
                                location.reload()

                             })
                        },
                        error:function(res){
                            console.log(res)
                        }
                    })

                })


                },
                error:function(res){
                    console.log(res)
                }
            })
        })

        //搜索用户
        $('.findUser').on('click',function(){
            var html='';
                    html=`
                    <input type="text" class="FU"></input>
                    <input type="button" class="FUBtn" value="查询用户">
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有用户</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i> 用户ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 用户名</th>
                                    <th><i class="fa fa-bookmark">注册时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
                    `
        $('.mainMiddle').html(html)
        $('.FUBtn').on('click',function(){
            var fidVal=$('.FU').val()
            console.log(fidVal)
            console.log(1111)
            $.ajax({
                url:'/admin/findUser',
                type:'post',
                data:{
                    findVal:fidVal
                },
                success:function(res){
                    console.log(res);
                    
                    var html1=''
                    for(let i=0;i<res.data.length;i++){
                        html1+=`
                        <tr class="${res.data[i]._id}">
                        <td><a href="javascript:;">${res.data[i]._id}</a></td>
                        <td class="hidden-phone">${res.data[i].username}</td>
                        <td>${res.data[i].regDate}</td>
                        <td><span class="label label-info label-mini">Due</span></td>
                        <td>
                            <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                            <button class="btn btn-primary btn-xs "><i class="fa fa-pencil"></i></button>
                            <button class="btn btn-danger btn-xs deleU"><i class="fa fa-trash-o "></i></button>
                        </td>
                    </tr>
                        
                        `

                    }
                    $('.mainMiddle .content-panel tbody').html(html1)
                    //搜索成功后删除用户
                $('.deleU').on('click',function(evt){
                    var id=$(evt.target).parent().parent().attr('class')
                    deleUser(id,evt.target)
                  })



                }
            })
        })
            

        })

        //搜索文章

        $('.findArcticle').on('click',function(){
            var html='';
                    html+=`
                    <input type="text" class="FA"></input>
                    <input type="button" class="FABtn" value="搜索文章">
                    <div class="row mt">
                    <div class="col-md-12">
                        <div class="content-panel">
                            <table class="table table-striped table-advance table-hover">
                                    <h4><i class="fa fa-angle-right"></i>所有文章</h4>
                                    <hr>
                                <thead>
                                <tr>
                                    <th><i class="fa fa-bullhorn"></i>文章ID</th>
                                    <th class="hidden-phone"><i class="fa fa-question-circle"></i> 文章标题</th>
                                    <th><i class="fa fa-bookmark">文章发布时间</i> </th>
                                    <th><i class=" fa fa-edit"></i> Status</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
  
          </section>
        </section>
            `
            $('.mainMiddle').html(html)
            $('.FABtn').on('click',function(){
               var findVal= $('.FA').val()
               console.log(findVal)
                $.ajax({
                    url:'/admin/findArcticle',
                    type:'post',
                    data:{
                        findVal:findVal
                    },
                    success:function(res){
                       // console.log('1qqq')
                        console.log(res)
                        
                var html1=''
                for(var i=0;i<res.data.length;i++){
                    html1+=`
                    <tr class="${res.data[i]._id}">
                    <td><a href="javascript:;">${res.data[i]._id}</a></td>
                    <td class="hidden-phone">${res.data[i].arctitle}</td>
                    <td>${res.data[i].pubtime}</td>
                    <td><span class="label label-info label-mini">Due</span></td>
                    <td>
                        <button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
                        <button class="btn btn-primary btn-xs changeA"><i class="fa fa-pencil"></i></button>
                        <button class="btn btn-danger btn-xs deleA"><i class="fa fa-trash-o "></i></button>
                    </td>
                </tr>
                    `
                }
                    $('.mainMiddle .content-panel tbody').html(html1)
                    //查询文章成功后删除文章
                    $('.deleA').on('click',function(evt){
                        var id=$(evt.target).parent().parent().attr('class')
                        
                        $.ajax({
                            url:'/admin/deleArticle',
                            type:'post',
                           data:{
                            id:id,
                           },
                            success:function(res){
                                console.log(res)
                               $(evt.target).parent().parent().remove()
                            },
                            error:function(res){
                                console.log(res)
                            }
    
                        })
                    })//删除文章结束
                    //修改文章
                    $('.changeA').on('click',function(evt){
                        
                        var id=$(evt.target).parent().parent().attr('class')
                        $('.mengban').css({
                            "display":"block"
                        })
                        $('.del').on('click',function(){
                            $('.mengban').css({
                                'display':'none'
                            })
                        })
                        //查询特定的文章
                        console.log(id)
                        $.ajax({
                            url:'/admin/readArc',
                            type:'post',
                            data:{
                                id:id,
                            },
                            success:function(res){
                                console.log(res)
                                console.log(res.data[0].arctitle,res.data[0].content)
                                $('.uptitle').val(res.data[0].arctitle)
                                 ue.setContent(res.data[0].content);
                                 
                                 //查询文章之后修改文章
                                 $('.upBtn').on('click',function(){
                                    var arctitle=$('.uptitle').val()
                                    var content=ue.getContent()
                                    console.log(content,arctitle)
                                    $.ajax({
                                        url:'/admin/uploadArc',
                                        type:'post',
                                        data:{
                                            id:id,
                                           arctitle:arctitle,
                                           content:content
                                        },
                                        success:function(res){
                                           console.log(res)
                                        }
                                    })
                                    $('.mengban').css({
                                        'display':'none'
                                    })
                                    location.reload()
    
                                 })
                            },
                            error:function(res){
                                console.log(res)
                            }
                        })
    
                    })
    
    
                    },
                    error:function(res){
                        console.log(res)
                    }
                })

            })
            
        })







    
})